import numpy as np
import matplotlib.pyplot as plt


def minkowski_distance(p1, p2, p):
    return np.sum(np.abs(p1 - p2) ** p) ** (1 / p)


# 创建数据点
x = np.linspace(0, 10, 100)
y = np.linspace(0, 10, 100)
X, Y = np.meshgrid(x, y)

# 选择两个点
p1 = np.array([2, 2])
p2 = np.array([8, 8])

# 计算不同p值的距离
p_values = [1, 2, 10]

fig, axs = plt.subplots(1, 3, figsize=(20, 6))

for i, p in enumerate(p_values):
    Z = ((np.abs(X - p1[0]) ** p + np.abs(Y - p1[1]) ** p) ** (1 / p))

    axs[i].contourf(X, Y, Z, levels=20, cmap='viridis')
    axs[i].plot(p1[0], p1[1], 'ro', markersize=10, label='p1')
    axs[i].plot(p2[0], p2[1], 'bo', markersize=10, label='p2')
    axs[i].plot([p1[0], p2[0]], [p1[1], p2[1]], 'r--', linewidth=2)
    axs[i].set_title(f'Minkowski Distance (p={p})')
    axs[i].set_xlabel('X')
    axs[i].set_ylabel('Y')
    axs[i].legend()

plt.tight_layout()
plt.show()

for p in p_values:
    print(f"Minkowski distance (p={p}) between p1 and p2: {minkowski_distance(p1, p2, p):.2f}")